package com.example.testappwidget.model

import android.content.Context
import android.content.Intent
import android.os.BatteryManager
import androidx.annotation.Keep

/**
 *Author: chinadragon
 *Time: 2024/3/27
 */
@Keep
class BatteryWidgetInfo {

    var batteryPresent: Boolean = false                // 电池是否存在

    var batteryStatus: Int = BATTERY_UNKNOWN           // 电池状态(1:未知;2:充电;3:放电;4:不充电;5:满电)

//    var batteryHealth: Int = BATTERY_UNKNOWN           // 电池健康状态(1:未知;2:良好;3:过热;4:死亡;5:过电压;6:未指定故障;7:过冷)

//    var batteryPlugged: Int = BATTERY_UNKNOWN           // 电池充电方式(1:AC;2:USB;4:Wireless) BatteryManager.BATTERY_PLUGGED_AC or BatteryManager.BATTERY_PLUGGED_USB or BatteryManager.BATTERY_PLUGGED_WIRELESS

    var batteryLevel: Int = BATTERY_UNKNOWN             // 电池电量

    var batteryScale: Int = BATTERY_UNKNOWN             // 电池总电量

//    var batteryCapacity: Int = BATTERY_UNKNOWN          // 电池电量整数百分比

//    var batteryVoltage: Int = BATTERY_UNKNOWN           // 电池电压

//    var batteryTemperature: Int = BATTERY_UNKNOWN       // 电池温度

//    var batteryTechnology: String? = null               // 电池技术


    //文字颜色
    var textColor: String? = "#08FF08"


    /**
     * 是否充电
     * return true: 表示设备充电状态；false：表示设备不是充电状态
     */
    fun charge(): Boolean{
        return batteryStatus == BatteryManager.BATTERY_STATUS_CHARGING
    }


    companion object {
        const val BATTERY_UNKNOWN = -1
        const val BATTERY_STATUS_UNKNOWN = BatteryManager.BATTERY_STATUS_UNKNOWN
        const val BATTERY_HEALTH_UNKNOWN = BatteryManager.BATTERY_HEALTH_UNKNOWN

        fun generate(context: Context, registerReceiverIntent: Intent): BatteryWidgetInfo {
//            val batteryManager = ContextCompat.getSystemService(context, BatteryManager::class.java)
//            val capacity = batteryManager?.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) ?: BATTERY_UNKNOWN
            return BatteryWidgetInfo().apply {
                batteryPresent = registerReceiverIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false)
                batteryStatus = registerReceiverIntent.getIntExtra(BatteryManager.EXTRA_STATUS, BATTERY_STATUS_UNKNOWN)
//                batteryHealth = registerReceiverIntent.getIntExtra(BatteryManager.EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN)
                //BatteryManager.BATTERY_PLUGGED_AC or BatteryManager.BATTERY_PLUGGED_USB or BatteryManager.BATTERY_PLUGGED_WIRELESS
//                batteryPlugged = registerReceiverIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_UNKNOWN)
                batteryLevel = registerReceiverIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, BATTERY_UNKNOWN)
                batteryScale = registerReceiverIntent.getIntExtra(BatteryManager.EXTRA_SCALE, BATTERY_UNKNOWN)
//                batteryVoltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, BATTERY_UNKNOWN)
//                batteryTemperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, BATTERY_UNKNOWN)
//                batteryTechnology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)
//                batteryCapacity = if (capacity == BATTERY_UNKNOWN) (batteryLevel * 100f / batteryScale).toInt() else capacity
            }
        }
    }

}